//index.js
//获取应用实例
var app = getApp()
// 语言
var util = require('../../utils/util.js')
import event from '../../utils/event'

Page({
  data: {
    //语言 - begin
    categoryId: '',
    page: 0,
    goods: [],
    isLoadProduct: false,
    loadingMoreHidden:false
    //语言 - end
  },

  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-detail/goods-detail?id=" + e.currentTarget.dataset.id
    })
  },
  onLoad: function (e) {
    wx.showLoading();

    var that = this;
    that.setData({
      categoryId: e.id,
      name:e.name||'分类列表'
    });
    
    
    if (app.globalData.iphone == true) { that.setData({ iphone: 'iphone' }) }
    if(e.id==0){
      // console.log("9-9包邮")
      this.nightGoods();
    }else{
      that.fetchCategory();
    }
    
    wx.T.setLocaleByIndex(wx.T.langIndex);
    // 语言 - 结束
  },
  nightGoods: function () {
    var that = this;
    that.setData({
      page: that.data.page + 1
    });
    wx.request({
      url: app.globalData.urls + '/product/nine-list',
      header: app.getRequestHeader(),
      data: {
        page:this.data.page,
        bdmin_user_id:util.share_bdmin_user_id()
      },
      success: function (res) {
        wx.hideLoading();
        let data  = that.data.goods.concat(res.data.data) 
        that.setData({
          goods: data,
          isLoadProduct:false
        });
        if(data.length==0){
          that.setData({
            loadingMoreHidden: true
          });
        }
        app.saveReponseHeader(res);
      }
    })
  },
  fetchCategory: function () {
    var that = this;
    that.setData({
      page: that.data.page + 1
    });
    wx.request({
      url: app.globalData.urls + '/product/category-search',
      header: app.getRequestHeader(),
      data: {
        bdmin_user_id:util.share_bdmin_user_id(),
        categoryId: that.data.categoryId,
        p: that.data.page
      },
      success: function (res) {
        wx.hideLoading();
        var goods = that.data.goods;
        // console.log("商品",goods)
        // for (var i = 0; i < res.data.data.products.length; i++) {
        //   goods.push(res.data.data.products[i]);
        // }
        that.setData({
          goods: goods.concat(res.data.data.products),
          isLoadProduct: false,
        });
        if(that.data.goods.length==0){
          that.setData({
            loadingMoreHidden: true
          });
        }
        app.saveReponseHeader(res);
      }
    })
  },
  loadProduct: function(){

    var that = this
    console.log("that.data.isLoadProduct",that.data.isLoadProduct)
    if (that.data.isLoadProduct == false) {
      that.setData({
        isLoadProduct: true,
      });
      if(that.data.categoryId==0){
        this.nightGoods();
      }else{
        that.fetchCategory()
      }
    }
    
  }

})
